package com.cloudptt.api.product.func.sys.fsm;

import android.content.Context;

import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.sys.SysFunc;
import com.cloudptt.api.product.func.sys.SysStateInfo;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.timer.IRollback;
import com.cloudptt.api.product.timer.RollbackTimer;

import java.util.Stack;

public class SipPublishSucState implements ISysState, IRollback {

	private static final int TimerId_Stack 					= 1;
	private static final int TimerId_Publish             	= 2;
	private Context ctx = null;
	private SysAgent sysAgent = null;
	private RollbackTimer cmsRollbackTimer = null;
	Stack<Integer> cmsGmsStack = new Stack<>();
	private RollbackTimer rollbackTimer = null;

	public SipPublishSucState(Context ctx, SysAgent sysAgent,long expires,boolean isRefresh)
	{
		Log.d(SysFunc.Log_Key, "SipPublishSucState expires = " + expires + " isRefresh = " + isRefresh);
		this.ctx = ctx;
		this.sysAgent = sysAgent;
		long val = expires*2/3;
		Log.d(SysFunc.Log_Key, "SipPublishSucState val = " + val);
		this.rollbackTimer = new RollbackTimer(ctx,TimerId_Publish,val*10,this);
		if(!isRefresh) {
			startCmsGmsTimer();
		}
	}
	@Override
	public int doSysAction(int sysAction) {
		// TODO Auto-generated method stub
		if(SysConst.SysAction_Logout == sysAction)
		{
			this.sysAgent.doSipUnAuth();
			this.sysAgent.goToState(SysStateInfo.SysState_SvrStartState,SysStateInfo.Cause_Logout);
		}
		return 0;
	}

	@Override
	public void recevSysMsg(int sysMsg) {
		// TODO Auto-generated method stub
		Log.d(SysFunc.Log_Key, "SipAuthingState recevSysMsg sysMsg = " + sysMsg);
		if(SysConst.SysMsg_Svr_Stop == sysMsg)
		{
			this.sysAgent.goToState(SysStateInfo.SysState_SvrStopState,SysStateInfo.Cause_Suc);
		}
//		else if(sysMsg == SysConst.SysMsg_Sip_Auth_Suc)
//		{
//			sysAgent.goToState(SysStateInfo.SysState_SipAuthSucState,SysStateInfo.Cause_Suc);
//		}
		else if(sysMsg == SysConst.SysMsg_Sip_Auth_Failed)
		{
			sysAgent.goToState(SysStateInfo.SysState_SvrStartState,SysStateInfo.Cause_Failed);
		}
		else if(sysMsg == SysConst.SysMsg_Sip_Auth_Timeout)
		{
			sysAgent.goToState(SysStateInfo.SysState_SvrStartState,SysStateInfo.Cause_Timeout);
		}
		else if(SysConst.SysMsg_Logout_Suc == sysMsg)
		{
			this.sysAgent.goToState(SysStateInfo.SysState_SvrStartState,SysStateInfo.Cause_Logout);
		}
	}
	@Override
	public void recevHttpResult(boolean result,int code) {
		// TODO Auto-generated method stub
	}
	@Override
	public int recevPublishResult(int mcType,int publishCount,boolean isSuc)
	{
		Log.d(SysFunc.Log_Key,"recevPublishResult mcType = " +mcType +  " publishCount = " +publishCount +  " isSuc = " +isSuc);
		return 0;
	}

	@Override
	public int getState() {
		// TODO Auto-generated method stub
		return SysStateInfo.SysState_SipPulishSucState;
	}

	@Override
	public void destory() {
		// TODO Auto-generated method stub
		if(cmsRollbackTimer != null)
		{
			cmsRollbackTimer.cancelTimer();
			cmsRollbackTimer = null;
		}
		if(rollbackTimer != null)
		{
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}
		sysAgent = null;
	}

	@Override
	public void rollback(int timerId) {
		Log.d(SysFunc.Log_Key, SysStateInfo.stateTag(this.getState()) + " rollback timerId = " + timerId);
		if(TimerId_Publish == timerId)
		{
			this.sysAgent.doHttpRefresh();
			this.sysAgent.goToState(SysStateInfo.SysState_HttpRefreshingState,SysStateInfo.Cause_Suc);
		}
		else
		{
			int type = cmsGmsStack.pop();
			this.sysAgent.doSubcribeCms(type);
			if (cmsGmsStack != null
					&& cmsRollbackTimer != null) {
				if (cmsGmsStack.empty()) {
					cmsRollbackTimer.cancelTimer();
				} else {
					cmsRollbackTimer.restartTimer(2);
				}
			}
		}
	}
	private void startCmsGmsTimer()
	{
		Log.d(SysFunc.Log_Key,"startCmsGmsTimer");
		// 压入元素到栈中
		cmsGmsStack.push(CallAttr.McType_McData);
		cmsGmsStack.push(CallAttr.McType_McVideo);
		cmsGmsStack.push(CallAttr.McType_McPtt);
		if(cmsRollbackTimer != null)
		{
			cmsRollbackTimer.cancelTimer();
		}

		this.cmsRollbackTimer = new RollbackTimer(ctx,TimerId_Stack,2,this);
	}
}
